/*
 * @(#)PageController.java
 * PageController类作为SpringMVC的Controller层
 * 主要负责对页面方面的请求的映射处理，调用Service层的方法对请求进行处理并返回结果
 * Copyright 2015 上海领智信息科技有限公司, Inc. All rights reserved.
 * @author reeves
 * @Time 2015/04/20 21:37:00
 * @version 1.0
 */

package com.version1_2.controller;

import com.alibaba.fastjson.JSONObject;
import com.util.format.MapToJson;
import com.util.property.Properties;
import com.version1_1.controller.PageController;
import com.version1_2.service.PageService1_2;
import com.version1_3.service.CoinService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
public class PageController1_2 {
    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(PageController1_2.class);

    @Autowired
    private PageService1_2 pageService12;

    @Autowired
    private PageController pageController; // 复用version1.1中的部分Controller接口

    @Autowired
    private CoinService coinService;

    /*
    * 更新内容:
    * 1. 未登陆用户根据分页数得到主页面信息; (requestbody&服务器返回信息格式)
    * 2. 改变评论的点赞数; (reqeustbody)
    * 3. 新增一条评论或回复; (requestbody)
    * 4. 在文章中对三级分类信息进行评论和提供信息源; (requestbody)
    * 5. 登陆用户根据分页数得到主页面信息; (服务器返回信息格式)
    * 6. 获取除正文之外的其他信息; (服务器返回信息格式)
    * 7. 根据分页数得到三级分类页面的预览信息; (服务器返回信息格式)
    * 8. 搜索文章标题; (服务器返回信息格式)
    * */


    /**
     * @Description: 搜索文章标题
     */
    @RequestMapping(value = "/version1_2/page/search/{key}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String searchPage(@PathVariable("key") String key) throws UnsupportedEncodingException {
        return pageService12.searchPage(key);
    }

    /**
     * 调用接口: http://139.129.18.232/kuibuversion1/version1_2/page/thirdPage/{secondType}/{thirdTypeName}/{pageNumber}
     *
     * @Description: 根据分页数得到三级分类页面的预览信息
     */
    @RequestMapping(value = "/version1_2/page/thirdPage/{secondType}/{thirdTypeName}/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getThirdPage(@PathVariable("secondType") int secondType,
                               @PathVariable("pageNumber") int pageNumber,
                               @PathVariable("thirdTypeName") String thirdTypeName)
            throws UnsupportedEncodingException {
        return pageService12.getThirdPage(secondType,
                thirdTypeName, pageNumber, Properties.PageSize_ThirdPage);
    }


    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/newMainPage/{itemNumber}/{userId}/{pageNumber}/{cacheTag}
     *
     * @Description: 登陆用户根据分页数得到主页面信息
     * @description: 图片传递url
     */
    @RequestMapping(value = "/version1_2/page/newMainPage/{itemNumber}/{userId}/{pageNumber}/{cacheTag}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getNewMainPage(@PathVariable("userId") int userId,
                                 @PathVariable("itemNumber") int firstType,
                                 @PathVariable("pageNumber") int pageNumber,
                                 @PathVariable("cacheTag") boolean cacheTag) {

        return pageService12.getNewMainPage(userId, firstType,
                pageNumber, Properties.Size_MainPageThird, cacheTag);
    }

    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/getPageOtherInfo/{userId}/{pageId}
     *
     * @Description: 获取除正文之外的其他信息
     */
    @RequestMapping(value = "/version1_2/page/getPageOtherInfo/{userId}/{pageId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getPageOtherInfo(@PathVariable("pageId") int pageId, @PathVariable("userId") int userId) {

        return pageService12.getPageOtherInfo(userId, pageId);
    }

    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/newMainPageNoLogin/{pageNumber}/{cacheTag}
     *
     * @Description: 未登陆用户根据分页数得到主页面的预览信息
     */
    @RequestMapping(value = "/version1_2/page/newMainPageNoLogin/{pageNumber}/{cacheTag}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getNewMainPageNoLogin(HttpServletRequest request, @RequestBody JSONObject jsonObject,
                                        @PathVariable("pageNumber") int pageNumber, @PathVariable("cacheTag") boolean cacheTag)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");


        if (jsonObject == null) {
            LOGGER.error("未登录用户主页面信息获取错误,请求mainFocusList为null!");
            return "{\"isLast\":true}";
        }

        return pageService12.getNewMainPageNoLogin(jsonObject, pageNumber, Properties.Size_MainPageThird, cacheTag);
    }

    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/comPraise
     *
     * @Description: 改变评论的点赞数
     */
    @RequestMapping(value = "/version1_2/page/comPraise", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String pageComPraise(HttpServletRequest request, @RequestBody JSONObject jsonObject)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        if (jsonObject == null) {
            return "{\"commentOK\":false}";
        }

        return pageService12.setPageComPraise(jsonObject);
    }

    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/addComment
     *
     * @Descrition: 新增一条评论或回复, 增加积分记录
     */
    @RequestMapping(value = "/version1_2/page/addComment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String pageComment(HttpServletRequest request, @RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        if (jsonObject == null) {
            LOGGER.error("新增一条评论错误,未接收到客户端参数!");
            return "{\"commentOK\":false}";
        }

        if (jsonObject.getIntValue("senderId") != -1) {
            // 只有登陆用户才记录评论
            coinService.moduleRecord(jsonObject.getIntValue("senderId"), "information", "commentInPageOrReply", 10, 1, 20); // 评论文章增加积分
        }
        return pageService12.setPageComment(jsonObject);
    }


    /**
     * 调用接口：http://139.129.18.232/kuibuversion1/version1_2/page/postComInPage
     *
     * @Description: 在文章中对三级分类信息进行评论和提供信息源
     */
    @RequestMapping(value = "/version1_2/page/postComInPage", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postComInPage(HttpServletRequest request, @RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        boolean result = false;

        // 若没有接收到客户端发送的参数
        if (jsonObject == null) {
            LOGGER.error("在文章中对三级分类信息进行评论和提供信息源出错,无法获得客户端发送参数: comInPageStr");
        } else {
            result = pageService12.postComInPage(jsonObject);
        }
        return new MapToJson("postComInPageResult", result).toJsonString();
    }

    /**
     * @Description: 根据pageId, 得到指定页面的信息数据 OK
     */
    @RequestMapping(value = "/version1_2/page/getPageInfoShare/{pageId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getPageInfoShare(@PathVariable("pageId") int pageId) {
        return pageService12.getPageInfoShare(pageId);
    }


    /**
     * @Description: 根据pageId, 得到指定页面的信息数据,增加积分记录
     */
    @RequestMapping(value = "/version1_2/page/getPageInfo/{pageId}/{userId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getPageInfo(HttpServletRequest request, @PathVariable("pageId") int pageId, @PathVariable("userId") int userId) {

        if (userId != -1) {
            // 登陆用户才记录用户阅读
            coinService.moduleRecord(userId, "information", "readArticle", 5, 1, 20); // 阅读文章增加积分
        }
        return pageController.getPageInfo(request, pageId);
    }

    /**
     * @Description: 记录一次文章被转发(分享)
     */
    @RequestMapping(value = "/version1_2/page/transmit/{pageId}/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String doTransmitCount(@PathVariable("pageId") int pageId, @PathVariable("userId") int userId) {
        /**
         * 记录文章被转发(分享)
         */

        if (userId != -1) {
            // 登陆用户才记录用户阅读
            coinService.moduleRecord(userId, "information", "shareArticle", 10, 5, 20); // 阅读文章增加积分
        }
        return pageController.doTransmitCount(pageId);
    }

    /*------------------------------------------------- 复用version1.1中pageController的部分接口 ------------------------------------------------------------*/

    /**
     * @Description: 根据pageId, 得到指定页面的信息数据 OK
     */
    @RequestMapping(value = "/version1_2/page/getPageInfo/{pageId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getPageInfo(HttpServletRequest request, @PathVariable("pageId") int pageId) {
        return pageController.getPageInfo(request, pageId);
    }

    /**
     * @Description: 获取文章评论列表数据 device-error 加载不出来
     */
    @RequestMapping(value = "/version1_2/page/getCommentList/{userId}/{pageId}/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getCommentList(@PathVariable("userId") int userId,
                                 @PathVariable("pageId") int pageId,
                                 @PathVariable("pageNumber") int pageNumber)
            throws UnsupportedEncodingException {
        return pageController.getCommentList(userId, pageId, pageNumber);
    }

    /**
     * @Description: 记录文章被用户阅读 OK
     */
    @RequestMapping(value = "/version1_2/page/read/{pageId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String doRead(@PathVariable("pageId") int pageId) {
        return pageController.doRead(pageId);
    }

    /**
     * @Description: 记录一次文章被转发(分享)
     */
    @RequestMapping(value = "/version1_2/page/transmit/{pageId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String doTransmitCount(@PathVariable("pageId") int pageId) {
        /**
         * 记录文章被转发(分享)
         */
        return pageController.doTransmitCount(pageId);
    }

    /**
     * @Description: 对文章内容进行评论好文和水文, 增加积分记录
     */
    @RequestMapping(value = "/version1_2/page/pageContentComment/{userId}/{pageId}/{commentType}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String pageContentComment(@PathVariable("userId") int userId, @PathVariable("pageId") int pageId, @PathVariable("commentType") int commentType) {

        if (userId != -1 && commentType == 1) {
            coinService.moduleRecord(userId, "information", "goodOrBadArticle", 5, 1, 10); // 好文水文增加积分
        }
        return pageController.pageContentComment(userId, pageId, commentType);
    }

    /**
     * 获取评论回复信息列表 device-error 能发送回复,加载不出列表
     */
    @RequestMapping(value = "/version1_2/page/comReply/{commentId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getCommentReply(@PathVariable("commentId") int commentId) throws UnsupportedEncodingException {

        /**
         * 返回信息：comReplyStr
         * 格式：[{"replyerName":"","replyerPhoto":"","replyDate":"","replyContent":""}...]
         */
        return pageController.getCommentReply(commentId);
    }
}
